package com.funtester.seven.day0;

import java.util.stream.IntStream;


public class Output {

    private static final String UP = "~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~";

    private static final String DOWN = "~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~";

    /**
     * 输出，自带log方法,排除root用户使用输出
     *
     * @param text
     */
    public static void output(String text) {
        System.out.println(text);
    }

    private static final String[] SERIAL = {"⓪", "①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩", "⑪", "⑫", "⑬", "⑭", "⑮", "⑯", "⑰", "⑱", "⑲", "⑳"};

    static String getSerialEmoji(int i) {
        return SERIAL[i];
    }

    public static final String EMPTY = "";

    public static final String LINE = "\r\n";

    /**
     * 以JSON格式输出
     *
     * @param o
     */
    public static void outputJsonStr(String jsonStr) {
        jsonStr = jsonStr.replaceAll("\\\\/", "/");
        int level = 0;// 用户标记层级
        StringBuffer jsonResultStr = new StringBuffer("＞  ");// 新建stringbuffer对象，用户接收转化好的string字符串
        int length = jsonStr.length();
        for (int i = 0; i < length; i++) {// 循环遍历每一个字符
            char piece = jsonStr.charAt(i);// 获取当前字符
            // 如果上一个字符是断行，则在本行开始按照level数值添加标记符，排除第一行
            if ('\n' == jsonResultStr.charAt(jsonResultStr.length() - 1)) {
                jsonResultStr.append(getSerialEmoji(level) + " . ");
                IntStream.range(0, level - 1).forEach(x -> jsonResultStr.append(". . "));//没有采用sourcecode的getmanystring
            }
            char last = i == 0 ? '{' : jsonStr.charAt(i - 1);
            char next = i < length - 1 ? jsonStr.charAt(i + 1) : '}';
            switch (piece) {
                case ',':
                    // 如果是“,”，则断行
                    jsonResultStr.append(piece + (("\"0123456789le]}".contains(last + EMPTY) && "\"[{".contains(next + EMPTY)) ? LINE : EMPTY));
                    break;
                case '{':
                case '[':
                    // 如果字符是{或者[，则断行，level加1
                    jsonResultStr.append(piece + (":[{,".contains(last + EMPTY) && ",[{}]\"0123456789le".contains(next + EMPTY) ? LINE : EMPTY));
                    if (last != '[') level++;//解决jsonarray:[{
                    break;
                case '}':
                case ']':
                    // 如果是}或者]，则断行，level减1
//                    jsonResultStr.append(LINE);
                    jsonResultStr.append(("\"0123456789le]}{[,".contains(last + EMPTY) && "}],".contains(next + EMPTY) ? LINE : EMPTY));
                    if (next != ']') level--;//解决jsonarray:[{
                    jsonResultStr.append(level == 0 ? "" : getSerialEmoji(level) + " . ");
                    IntStream.range(0, level - 1).forEach(x -> jsonResultStr.append(". . "));//没有采用sourcecode的getmanystring
                    jsonResultStr.append(piece);
                    break;
                default:
                    jsonResultStr.append(piece);
                    break;
            }
        }
        output(LINE + UP + " JSON " + UP + LINE + jsonResultStr.toString().replaceAll(LINE, LINE + "＞  ") + LINE + DOWN + " JSON " + DOWN);
    }


}
